//
//  SportRecordItem.m
//  CoolTennisBall
//
//  Created by 谢伟康 on 15/12/24.
//  Copyright © 2015年 CoolLang. All rights reserved.
//

#import "SportRecordItem.h"
#import <DateTools/DateTools.h>
#import "NSDate+FormateString.h"

@implementation SportRecordItem

// Specify default values for properties
+ (NSDictionary *)defaultPropertyValues
{
    return @{
             @"dateTimeStamp":@0,
             @"dateStr":@"",
             @"calories":@0,
             @"exerciseTimeInterval":@0,
             @"maxSpeed":@0,
             @"pingJiCount":@0,
             @"xuanQiuCount":@0,
             @"xueQiuCount":@0,
             @"jieJiCount":@0,
             @"kouShaCount":@0,
             @"faQiuCount":@0,
             @"barChartItems":@[].mutableCopy,
             @"indexOfItem":@0,
             @"hasUploadSportSessionValues":@NO,
             @"hasReadItem":@NO,
             @"shouldShield": @NO,
             @"detailReportInfo":@"",
             @"isSyncDetailReportData":@NO,
             };
}

+ (NSArray *)ignoredProperties
{
    return @[@"indexOfItem"];
}

- (NSInteger)amountOfCount
{
    NSInteger amount = self.pingJiCount + self.xuanQiuCount + self.xueQiuCount + self.jieJiCount + self.kouShaCount + self.faQiuCount;
    return amount;
}

- (NSString *)monthAndDay {
    
    if (self.dateStr.length <= 0) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:NSLocalizedString(@"MM/dd", nil)];

        NSDate *date = [NSDate dateWithUnixTimeStamp:self.dateTimeStamp];
        if (![NSBundle isThisLanguages]) {
            NSTimeZone  *zone = [NSTimeZone systemTimeZone];
            NSTimeInterval time  =[zone secondsFromGMTForDate:date];
            date = [date dateByAddingTimeInterval:time];
            NSLog(@"dateTwo:%@",date);
            return [NSString stringWithFormat:@"%zd/%zd",[date month],[date day]];
        }else {
            return [dateFormatter stringFromDate:date];
        }
    }else {
        NSDate *date = [NSDate dateWithString:self.dateStr formatString:@"yyyy-MM-dd"];
        NSLog(@"%@",self.dateStr);
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:NSLocalizedString(@"MM/dd", nil)];
        return [dateFormatter stringFromDate:date];
    }
}

- (NSString *)yearAndMonthAndDay
{
    if (self.dateStr.length <= 0) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *date = [NSDate dateWithUnixTimeStamp:self.dateTimeStamp];
        //将获取后的本地时间 转换成东八区时间
        dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
        NSString *strDate = [dateFormatter stringFromDate:date];
        return strDate;
    }else {
        return self.dateStr;
    }
}

- (NSString *)yearAndMonth {
    if (self.dateStr.length <= 0) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM"];
        NSDate *date = [NSDate dateWithUnixTimeStamp:self.dateTimeStamp];
        if (![NSBundle isThisLanguages]) {
            NSTimeZone  *zone = [NSTimeZone systemTimeZone];
            NSTimeInterval time  =[zone secondsFromGMTForDate:date];
            date = [date dateByAddingTimeInterval:time];
            return [NSString stringWithFormat:@"%zd-%zd",[date year],[date month]];
        }else {
            return [dateFormatter stringFromDate:date];
        }
    }else {
        NSDate *date = [NSDate dateWithString:self.dateStr formatString:@"yyyy-MM-dd"];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM"];
        return [dateFormatter stringFromDate:date];
    }
}

- (NSInteger)calculatedCalories
{
    User *user = [User getLastUser];
    return 420 * [user.userInforItem.weight integerValue] * self.exerciseTimeInterval /3600;
}
- (NSDate *)date
{
    return [NSDate dateWithUnixTimeStamp:self.dateTimeStamp];
}

- (void)analysisItemWithDictionary:(NSDictionary *)dic
{
    self.dateTimeStamp = [dic[@"DateTimestamp"] longLongValue];
    self.dateStr = dic[@"Date"];
    self.calories = [dic[@"Caroline"] integerValue];
    self.exerciseTimeInterval = [dic[@"Duration"] integerValue];
    self.maxSpeed = [dic[@"MaxSpeed"] integerValue];
    self.pingJiCount = [dic[@"TypeCount"][@"PingJi"] integerValue];
    self.xuanQiuCount = [dic[@"TypeCount"][@"XuanQiu"] integerValue];
    self.xueQiuCount = [dic[@"TypeCount"][@"XueQiu"] integerValue];
    self.jieJiCount = [dic[@"TypeCount"][@"JieJi"] integerValue];
    self.kouShaCount = [dic[@"TypeCount"][@"GaoYa"] integerValue];
    self.faQiuCount = [dic[@"TypeCount"][@"FaQiu"] integerValue];
    self.shouldShield = [dic[@"State"] integerValue] == 1;
    NSArray *jsonArr = dic[@"DetailData"];
    for (NSDictionary *swingDic in jsonArr) {
        BarChartItem *swingItem = [[BarChartItem alloc] init];
        [swingItem analysisItemWithDictionary:swingDic];
        [self.barChartItems addObject:swingItem];
    }
}

@end
